/*!
 * \file frct_hash_key.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frlog.h"
#include "frmem.h"
#include "frlist.h"
#include "frsutl.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frct_hash.h"
#include "frct_mng.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    int i_ret = 0;
    u_short us_proto = 6;
    u_int ui_src = iptools_parse_ipaddr((char *)"1192.168.1.1");
    u_short us_sport = 22;
    u_int ui_dest = iptools_parse_ipaddr((char *)"1192.168.0.1");
    u_short us_dport = 80;
    char *p_key = NULL;
    unsigned int ui_key_size = 0;


    i_ret = frct_hash_key(us_proto, ui_src, us_sport, ui_dest, us_dport, p_key, ui_key_size);

    CU_ASSERT_EQUAL(i_ret,0);
    CU_ASSERT_PTR_NULL(p_key);

}


static void __case2(void)
{
    int i_ret = 0;
    u_short us_proto = 6;
    u_int ui_src = iptools_parse_ipaddr((char *)"1192.168.1.1");
    u_short us_sport = 22;
    u_int ui_dest = iptools_parse_ipaddr((char *)"1192.168.0.1");
    u_short us_dport = 80;
    char *p_key = NULL;
    unsigned int ui_key_size = 128;

    p_key =(char *)frmalloc(ui_key_size);
    memset(p_key,'\0',ui_key_size);

    i_ret = frct_hash_key(us_proto, ui_src, us_sport, ui_dest, us_dport, p_key, ui_key_size);

    CU_ASSERT_EQUAL(i_ret,14);
    CU_ASSERT_STRING_NOT_EQUAL(p_key,"\0");

    frfree(p_key);
}

static void __case3(void)
{
    int i_ret = 0;
    u_short us_proto = 6;
    u_int ui_src = iptools_parse_ipaddr((char *)"1192.168.1.1");
    u_short us_sport = 22;
    u_int ui_dest = iptools_parse_ipaddr((char *)"1192.168.0.1");
    u_short us_dport = 80;
    char *p_key = NULL;
    unsigned int ui_key_size = 13;

    p_key =(char *)frmalloc(ui_key_size);
    memset(p_key,'\0',ui_key_size);

    i_ret = frct_hash_key(us_proto, ui_src, us_sport, ui_dest, us_dport, p_key, ui_key_size);

    CU_ASSERT_EQUAL(i_ret,0);
    CU_ASSERT_STRING_EQUAL(p_key,"\0");

    frfree(p_key);
}

static void __case4(void)
{
    int i_ret = 0;
    u_short us_proto = 6;
    u_int ui_src = iptools_parse_ipaddr((char *)"1192.168.1.1");
    u_short us_sport = 22;
    u_int ui_dest = iptools_parse_ipaddr((char *)"1192.168.0.1");
    u_short us_dport = 80;
    char *p_key = NULL;
    unsigned int ui_key_size = 14;

    p_key =(char *)frmalloc(ui_key_size);
    memset(p_key,'\0',ui_key_size);

    i_ret = frct_hash_key(us_proto, ui_src, us_sport, ui_dest, us_dport, p_key, ui_key_size);

    CU_ASSERT_EQUAL(i_ret,0);
    CU_ASSERT_STRING_EQUAL(p_key,"\0");

    frfree(p_key);
}

static void __case5(void)
{
    int i_ret = 0;
    u_short us_proto = 6;
    u_int ui_src = iptools_parse_ipaddr((char *)"1192.168.1.1");
    u_short us_sport = 22;
    u_int ui_dest = iptools_parse_ipaddr((char *)"1192.168.0.1");
    u_short us_dport = 80;
    char *p_key = NULL;
    unsigned int ui_key_size = 15;

    p_key =(char *)frmalloc(ui_key_size);
    memset(p_key,'\0',ui_key_size);

    i_ret = frct_hash_key(us_proto, ui_src, us_sport, ui_dest, us_dport, p_key, ui_key_size);

    CU_ASSERT_EQUAL(i_ret,14);
    CU_ASSERT_STRING_NOT_EQUAL(p_key,"\0");

    frfree(p_key);
}

int add_suite_frct_hash_key(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frct_hash frct_hash_key()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);

    return FR_SUCC;
}
